package com.danskin.wechat.webapi;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.danskin.wechat.entity.CrmBrandInfo; 
import com.danskin.wechat.entity.CustInfoExView;
import com.danskin.wechat.entity.WxMediaPlatform;
import com.danskin.wechat.model.AccessResponse;
import com.danskin.wechat.service.CrmBrandInfoService;
import com.danskin.wechat.service.CustInfoExService;
import com.danskin.wechat.util.ConvertUtils;
import com.danskin.wechat.util.Page;

@RestController
@RequestMapping("api/member")
public class MemberInfoController extends BaseApiController {

	@Autowired
	public CustInfoExService custInfoExService;

	@Autowired
	public CrmBrandInfoService crmBrandInfoService;

	/**
	 * 根据POS会员卡号获取微信AppId
	 * 
	 * @param map
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "getAppId", method = RequestMethod.POST)
	public Object getAppId(@RequestBody Map<String, Object> map, HttpServletRequest request) throws Exception {

		if (false == this.isValid("brandId", map)) {
			return this.getError("brandId不能为空");
		}
		// else if (false == this.isValid("posCardNo", map)) {
		// return this.getError("posCardNo不能为空");
		// }
		String brandId = map.get("brandId").toString();
		// String posCardNo = map.get("posCardNo").toString();
		// CustInfoExView custInfo = custInfoExService.getCustInfoEx(brandId,
		// posCardNo);
		CrmBrandInfo brandInfo = crmBrandInfoService.getCrmBrandInfo(brandId);
		if (brandInfo == null) {
			return this.getError("品牌不存在");
		}
		WxMediaPlatform mp = this.getMP(brandInfo.getMpId());
		if (mp == null) {
			return this.getError("公众号不存在");
		}
		JSONObject data = new JSONObject();

		data.put("appId", mp.getAppId());

		return new AccessResponse<Object>("0", "ok", data);

		// return new AccessResponse<Object>("-1", "该会员不存在。");
	}

	@RequestMapping(value = "getIsActivateMemberCard/{appId}")
	public Object getIsActivateMemberCard(@RequestBody Map<String, Object> map, @PathVariable("appId") String appId,
			HttpServletRequest request) throws Exception {

		if (false == this.isValid("openId", map)) {
			return this.getError("openId不能为空");
		}
		String openId = map.get("openId").toString();
		boolean flag = custInfoExService.getCustActiveMemberCard(appId, openId);

		return new AccessResponse<Object>("0", "ok", flag ? "1" : "0");
	}
}
